package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.*;
import com.mbyte.easy.admin.service.IAudioService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-12
 */
@Service
public class AudioServiceImpl extends ServiceImpl<AudioMapper, Audio> implements IAudioService {

    @Autowired
    private AudioMapper audioMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AllotMapper allotMapper;

    @Autowired
    private CheckMapper checkMapper;

    @Autowired
    private RelationMapper relationMapper;

    @Autowired
    private DraftMapper draftMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private GroupMapper groupMapper;

    /**
     * @Author 张雪
     * @Description 查询登录用户信息
     * @Date 2019/12/10 9:59
     **/
    @Override
    public List<SysUser> allotBefore(String username) {
        return sysUserMapper.allotBefore(username);
    }

    /**
     * @Author 张雪
     * @Description 查询登录用户的全部下属用户
     * @Date 2019/12/10 10:00
     **/
    @Override
    public List<SysUser> selectAllWorker(String username) {
        return sysUserMapper.selectAllWorker(username);
    }

    /**
     * @return 受影响行数
     * @Author 张雪
     * @Description 批量分配
     * @Date 2019/12/10 16:39
     * @Param List<Long> ids
     **/
    @Override
    public int allotByIds(List<Long> ids) {
        List<Allot> allotList = new ArrayList<>();
//        for (Long id : ids) {
//            // 分配集合
//            allotList.add(new Allot().setDraftId(id)
//                    .setAllotId(userId)
//                    .setAllotnoId(allotUserId));
//        }
//        allotService.saveBatch(allotList);
        return allotMapper.allotByIds(ids);

    }

    @Override
    public Long selectDraftIdById(Long id) {
        return audioMapper.selectDraftIdById(id);
    }



    /**
    * @Author//宋昱
     * @Description //TODO
     * @Date 2019/12/16 21:08
            **/
    @Override
    public List<Long> selectIdByAudio(Long id) {
        return audioMapper.selectIdByAudio(id);}

    @Override
    public Long selectIdByUsername(String currentUsername) {
        return sysUserMapper.selectUserIdByUsername(currentUsername);
    }
/**
* @Author//宋昱
 * @Description //根据组号查询
 * @Date 2019/12/20 16:38
        **/
    @Override
    public List<String> selectByGroupid(String groupid) {
        return audioMapper.selectByGroupid(groupid);
    }

    /**
    * @Author//宋昱
     * @Description //查下属id
     * @Date 2019/12/20 19:58
            **/
    @Override
    public List selectAllotNoId(Long allotid) {
        return allotMapper.selectAllotNoId(allotid);
    }
    /**
     * @Author//songyu
     * @Description //group表audio表查询
     * @Date 2019/12/20 20:34
     **/
    @Override
    public List<String> selectGroupid(Long allotNoid) {
        return audioMapper.selectGroupid(allotNoid);
    }

    /**
     * @Author//宋昱
     * @Description //根据组号查询状态
     * @Date 2019/12/20 23:45
     **/
    @Override
    public List<Integer> selectStatus(String groupid) {
        return audioMapper.selectStatus(groupid);
    }

    @Override
    public String selectGroupidById(long id) {
        return audioMapper.selectGroupidById(id);
    }

    @Override
    public List<Long> selectIdByGroupId(String groupId) {
        return audioMapper.selectIdByGroupId(groupId);
    }

    @Override
    public SysUser getUserByUserName(String username) { return sysUserMapper.selectByUsername(username); }

    @Override
    public int insertCheck(Check check) {
        return checkMapper.insert(check);
    }

    @Override
    public List<Long> getChildrenIdByParentId(Long id) {
        return relationMapper.getChildrenIdByParentId(id);
    }

    @Override
    public Draft GetGroupIdById(Long id) {
        return draftMapper.selectById(id);
    }

    @Override
    public Audio selectAGroupIdById(Long id) { return audioMapper.selectById(id);}

    @Override
    public List<Check> getCheckById(Long id) { return checkMapper.getCheckById(id);}

    @Override
    public List<Allot> getAllotDraftIdByUserId(Long userId) {
        return allotMapper.getAllotDraftIdByUserId(userId);
    }

    @Override
    public List<Allot> getDraftByAllotnoId(Long userId) {
        return allotMapper.getDraftByAllotnoId(userId);
    }

    @Override
    public SysUser getUserIdByUserName(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public SysUser getUserNameByUserId(Long userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public Integer insertToAllot(Allot allot) { return allotMapper.insert(allot); }

    @Override
    public Integer insertToGroup(Group group) {
        return groupMapper.insert(group);
    }

    @Override
    public List<Long> getChildrenNameByChildrenIdAudio(Long parentId) {
        return userMapper.getChildrenNameByChildrenIdAudio(parentId);
    }

    @Override
    public List<String> lookTitle(String username) {
        return audioMapper.lookTitle(username);
    }

    @Override
    public IPage<Audio> listPage(IPage<Audio> page, QueryWrapper<Audio> queryWrapper) {
        return page.setRecords(this.baseMapper.listPage(page,queryWrapper));
    }

    @Override
    public IPage<Audio> pageList(IPage<Audio> page, QueryWrapper<Audio> queryWrapper) {
        return page.setRecords(this.baseMapper.pageList(page,queryWrapper));
    }
}
